package com.xiaoniu.forum.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xiaoniu.forum.common.constant.ForumConstant;
import com.xiaoniu.forum.common.event.ForumEvent;
import com.xiaoniu.forum.common.utils.*;
import com.xiaoniu.forum.dao.MemberNospeakDao;
import com.xiaoniu.forum.entity.MemberNospeakEntity;
import com.xiaoniu.forum.entity.vo.AppFollowFansNumVo;
import com.xiaoniu.forum.entity.vo.MemberVo;
import com.xiaoniu.forum.service.MemberMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoniu.forum.dao.MemberDao;
import com.xiaoniu.forum.entity.MemberEntity;
import com.xiaoniu.forum.service.MemberService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberNospeakDao memberNospeakDao;

    @Autowired
    private MemberMessageService messageService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils multiConditPage(CommonQueryVo queryVo) {
        //分页多条件查询所有用户
        QueryWrapper<MemberEntity> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryVo.getAnd()) || !StringUtils.isEmpty(queryVo.getOr())) {
            queryWrapper = QueryWrapperUtils.createQueryWrapper(queryVo, MemberEntity.class);
        }
        IPage<MemberEntity> page = this.page(new Query<MemberEntity>().getPage(queryVo.getPageParams()),queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 禁言
     */
    @Transactional
    @Override
    public void nospeak(MemberNospeakEntity nospeakEntity, String reason) {
        memberNospeakDao.insert(nospeakEntity);
        ForumEvent forumEvent = new ForumEvent();
        forumEvent
                .setTriggerId(ForumConstant.SYSTEM_ID+"")  //事件触发者（发送人）
                .setTargetId(nospeakEntity.getMemberId())  //事件目标者（接受者）
                .setAction(ForumConstant.NO_SPEAK_ACTION)  //事件类型
                .setData(ForumConstant.KEYNAME_ENTITY_TYPE,ForumConstant.USER_ENTITY_TYPE) //其他数据
                .setData(ForumConstant.KEYNAME_RESON,reason)//其他数据
                .setData(ForumConstant.KEYNAME_TIME_NUM,nospeakEntity.getTimeNum())
                .setData(ForumConstant.KEYNAME_TIME_UNIT,nospeakEntity.getTimeUnit());
        messageService.sendEvent(forumEvent);
    }

    /**
     * 解除禁言
     * @param memberId    用户id
     */
    @Transactional
    @Override
    public void normalSpeak(String memberId) {
        int result = memberNospeakDao.delete(new QueryWrapper<MemberNospeakEntity>().eq("member_id", memberId));
        if (result >0){
            ForumEvent forumEvent = new ForumEvent();
            forumEvent
                    .setTriggerId(ForumConstant.SYSTEM_ID+"")  //事件触发者（发送人）
                    .setTargetId(memberId)  //事件目标者（接受者）
                    .setAction(ForumConstant.NORMAL_SPEAK_ACTION)  //解除禁言
                    .setData(ForumConstant.KEYNAME_ENTITY_TYPE,ForumConstant.USER_ENTITY_TYPE); //其他数据
            messageService.sendEvent(forumEvent);
        }
    }

    /**
     *    多条件查询用户信息（带禁言信息）
     */
    @Override
    public PageUtils multiConditMemberVoList(CommonQueryVo queryVo) {
        PageUtils page = multiConditPage(queryVo);
        //把分页查询出的每个用户对应的禁言状态封装进去
        List<MemberEntity> members = (List<MemberEntity>) page.getList();
        QueryWrapper<MemberNospeakEntity> qw = new QueryWrapper<MemberNospeakEntity>().in("member_id",
                members.stream().map(MemberEntity::getId).collect(Collectors.toList())
        );
        List<MemberNospeakEntity> nospeaks = memberNospeakDao.selectList(qw);
        List<MemberVo> memberVos = new ArrayList<>();
        //给每一个用户设置禁言信息
        for (MemberEntity member:members){
            MemberVo memberVo = new MemberVo();
            MemberNospeakEntity item = CollectionUtils.findItem(nospeaks,(nospeak) -> {
                return nospeak.getMemberId().equals(member.getId());
            });
            memberVo.setNospeak(item);
            BeanUtil.copyProperties(member,memberVo);
            memberVos.add(memberVo);
        }
        page.setList(memberVos);
        return page;
    }

    @Override
    public void disableMemberById(String id) {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setId(id);
        memberEntity.setStatus("3");
        baseMapper.updateById(memberEntity);
    }




}